Ext.define('Seq.svg.Base', {

	constructor : function(canvas) {
		this.canvas = canvas;
	},

	getModel : function(e, t, xy) {
		if (!xy) {
			xy = e.getXY();
		}
		var model = Snap.getElementByPoint(xy[0], xy[1]);
		if ('svg' == model.type) {
			var me = this;
			xy = me.getXy(e);
			for (var i = 0, len = me.models.length; i < len; i++) {
				if (Snap.path.isPointInsideBBox(me.getBBox(me.models[i]), xy.x, xy.y)) {
					return me.models[i];
				}
			}
		}
		return model;
	},

	getBBox : function(model) {
		var bbox = Snap._.box(model.node.getBBox());
		var path = Snap.path.map(Ext.String.format('M{0},{1}L{2},{3}', bbox.x, bbox.y, bbox.x2, bbox.y2), model
		                .transform().totalMatrix);
		bbox = Snap.path.getBBox(path);
		return bbox;
	},

	getOffset : function(e) {
		var box = e.getBoundingClientRect(), doc = e.ownerDocument, body = doc.body, docElem = doc.documentElement, clientTop = docElem.clientTop
		        || body.clientTop || 0, clientLeft = docElem.clientLeft || body.clientLeft || 0, top = box.top
		        + (window.pageYOffset || docElem.scrollTop || body.scrollTop) - clientTop, left = box.left
		        + (window.pageXOffset || docElem.scrollLeft || body.scrollLeft) - clientLeft;
		return {
			x : left,
			y : top
		}
	},

	getXy : function(e) {
		var me = this, xy = e.getXY(), offset = me.getOffset(me.svg ? me.svg.node : me.canvas.svg.node);

		return {
			x : xy[0] - offset.x,
			y : xy[1] - offset.y
		}
	}
});